<?php
namespace app\admin\controller;

use app\admin\service\ArticleService;

class Article extends Base
{
    private $service;

    public function __construct()
    {
        parent::__construct();

        $this->service = new ArticleService;
    }

    public function index()
    {
        $param = [
            'pageNo' => input('pageNo',1,'int,trim'),
            'pageSize' => input('pageSize',10,'int,trim'),
            'searchData' => [

            ]
        ];
        $list = $this->service->index( $param );
        jsonMsg(200,'成功',$list);
    }

    public function info()
    {
        $id = input('id');
        $info = $this->service->info($id);
        jsonMsg(200,'成功',$info);
    }

    public function add()
    {
//        var_dump($this->requestData);
//        die;
        $param = $this->mapping();
        $param['create_id'] = $this->userInfo['id'];
        $res = $this->service->save(0,$param);
        if( $res ){
            jsonMsg(200,'成功');
        }
        jsonMsg(0,'失败');
    }

    public function edit()
    {
        $param = $this->mapping();
        $id = input('id',0);
        $param['update_id'] = $this->userInfo['id'];
        $res = $this->service->save($id,$param);
        if( $res ){
            jsonMsg(200,'成功');
        }
        jsonMsg(0,'失败');
    }

    public function del()
    {
        $id = input('id',0);
        $res = $this->service->del( $id );
        if( $res ){
            jsonMsg(200,'成功');
        }
        jsonMsg(0,'失败');
    }

    private function mapping()
    {
        $data = $this->requestData;
        $param = [
            'title' => !empty($data['title']) ? $data['title'] : '',
            'status' => !empty($data['status']) ? $data['status'] : 1,
            'is_delete' => !empty($data['is_delete']) ? $data['is_delete'] : 1,
            'sort' => !empty($data['sort']) ? $data['sort'] : 0,
            'pic' => !empty($data['pic']) ? $data['pic'] : '',
            'content' => !empty($data['content']) ? $data['content'] : '',
        ];
        return $param;
    }

}
